package com.shanghai.shanghaielectric.DataAdapter;

import android.util.Log;

import com.shanghai.shanghaielectric.Model.ReceiveDataModel;
import com.shanghai.shanghaielectric.Model.db.ReceiveDataModel_10s;
import com.shanghai.shanghaielectric.Model.ReportDateModel;
import com.shanghai.shanghaielectric.Model.db.ReportDateModel_1day;

import java.util.Date;
import java.util.List;

import shanghaielectric_lib.Utils.DoubleUtils;

/**
 * Created by 45000460 on 2017/12/21.
 */

public class ReportDataHelper {


    public ReportDateModel homeDataModelToReportData(ReceiveDataModel newReceiveDataModel, ReportDateModel oldReportDateModel) {
        ReportDateModel reportDateModel = new ReportDateModel();
        reportDateModel.date = System.currentTimeMillis();
        reportDateModel.date_systemPF_Max_All = oldReportDateModel.systemPF_Max_All > Double.valueOf(newReceiveDataModel.all_PF) ? oldReportDateModel.date_systemPF_Max_All : getCurrentData();
        reportDateModel.date_systemPF_Min_All = oldReportDateModel.systemPF_Min_All > Double.valueOf(newReceiveDataModel.all_PF) ? oldReportDateModel.date_systemPF_Max_All : getCurrentData();

        reportDateModel.systemPF_Max_All = oldReportDateModel.systemPF_Max_All > Double.valueOf(newReceiveDataModel.all_PF) ? oldReportDateModel.systemPF_Max_All : Double.valueOf(newReceiveDataModel.all_PF);
        reportDateModel.systemPF_Min_All = oldReportDateModel.systemPF_Min_All < Double.valueOf(newReceiveDataModel.all_PF) ? oldReportDateModel.systemPF_Min_All : Double.valueOf(newReceiveDataModel.all_PF);
        reportDateModel.systemPF_AVG_All = oldReportDateModel.systemPF_AVG_All + Double.valueOf(newReceiveDataModel.all_PF);


        reportDateModel.systemPF_Max_A = oldReportDateModel.systemPF_Max_A > Double.valueOf(newReceiveDataModel.grid_PF_a) ? oldReportDateModel.systemPF_Max_A : Double.valueOf(newReceiveDataModel.grid_PF_a);
        reportDateModel.systemPF_Max_B = oldReportDateModel.systemPF_Max_B > Double.valueOf(newReceiveDataModel.grid_PF_b) ? oldReportDateModel.systemPF_Max_B : Double.valueOf(newReceiveDataModel.grid_PF_b);
        reportDateModel.systemPF_Max_C = oldReportDateModel.systemPF_Max_C > Double.valueOf(newReceiveDataModel.grid_PF_c) ? oldReportDateModel.systemPF_Max_C : Double.valueOf(newReceiveDataModel.grid_PF_c);

        reportDateModel.systemPF_Min_A = oldReportDateModel.systemPF_Min_A < Double.valueOf(newReceiveDataModel.grid_PF_a) ? oldReportDateModel.systemPF_Min_A : Double.valueOf(newReceiveDataModel.grid_PF_a);
        reportDateModel.systemPF_Min_B = oldReportDateModel.systemPF_Min_B < Double.valueOf(newReceiveDataModel.grid_PF_b) ? oldReportDateModel.systemPF_Min_B : Double.valueOf(newReceiveDataModel.grid_PF_b);
        reportDateModel.systemPF_Min_C = oldReportDateModel.systemPF_Min_C < Double.valueOf(newReceiveDataModel.grid_PF_c) ? oldReportDateModel.systemPF_Min_C : Double.valueOf(newReceiveDataModel.grid_PF_c);


        reportDateModel.systemPF_AVG_A = DoubleUtils.formatDoubleData(oldReportDateModel.systemPF_AVG_A + Double.valueOf(newReceiveDataModel.grid_PF_a) / 2, 2);
        reportDateModel.systemPF_AVG_B = DoubleUtils.formatDoubleData(oldReportDateModel.systemPF_AVG_B + Double.valueOf(newReceiveDataModel.grid_PF_b) / 2, 2);
        reportDateModel.systemPF_AVG_C = DoubleUtils.formatDoubleData(oldReportDateModel.systemPF_AVG_C + Double.valueOf(newReceiveDataModel.grid_PF_c) / 2, 2);

        reportDateModel.loadPF_Max_A = oldReportDateModel.loadPF_Max_A > Double.valueOf(newReceiveDataModel.load_power_factor_a) ? oldReportDateModel.loadPF_Max_A : Double.valueOf(newReceiveDataModel.load_power_factor_a);
        reportDateModel.loadPF_Max_B = oldReportDateModel.loadPF_Max_B > Double.valueOf(newReceiveDataModel.load_power_factor_b) ? oldReportDateModel.loadPF_Max_B : Double.valueOf(newReceiveDataModel.load_power_factor_b);
        reportDateModel.loadPF_Max_C = oldReportDateModel.loadPF_Max_C > Double.valueOf(newReceiveDataModel.load_power_factor_c) ? oldReportDateModel.loadPF_Max_C : Double.valueOf(newReceiveDataModel.load_power_factor_c);

        reportDateModel.loadPF_Min_A = oldReportDateModel.loadPF_Min_A < Double.valueOf(newReceiveDataModel.load_power_factor_a) ? oldReportDateModel.loadPF_Min_A : Double.valueOf(newReceiveDataModel.load_power_factor_a);
        reportDateModel.loadPF_Min_B = oldReportDateModel.loadPF_Min_B < Double.valueOf(newReceiveDataModel.load_power_factor_b) ? oldReportDateModel.loadPF_Min_B : Double.valueOf(newReceiveDataModel.load_power_factor_b);
        reportDateModel.loadPF_Min_C = oldReportDateModel.loadPF_Min_C < Double.valueOf(newReceiveDataModel.load_power_factor_c) ? oldReportDateModel.loadPF_Min_C : Double.valueOf(newReceiveDataModel.load_power_factor_c);

        reportDateModel.loadPF_AVG_A = DoubleUtils.formatDoubleData(oldReportDateModel.loadPF_AVG_A + Double.valueOf(newReceiveDataModel.load_power_factor_a) / 2, 2);
        reportDateModel.loadPF_AVG_B = DoubleUtils.formatDoubleData(oldReportDateModel.loadPF_AVG_B + Double.valueOf(newReceiveDataModel.load_power_factor_b) / 2, 2);
        reportDateModel.loadPF_AVG_C = DoubleUtils.formatDoubleData(oldReportDateModel.loadPF_AVG_C + Double.valueOf(newReceiveDataModel.load_power_factor_c) / 2, 2);


        reportDateModel.systemActivePower_Max_A = oldReportDateModel.systemActivePower_Max_A > Double.valueOf(newReceiveDataModel.grid_active_power_a) ? oldReportDateModel.systemActivePower_Max_A : Double.valueOf(newReceiveDataModel.grid_active_power_a);
        reportDateModel.systemActivePower_Max_B = oldReportDateModel.systemActivePower_Max_B > Double.valueOf(newReceiveDataModel.grid_active_power_b) ? oldReportDateModel.systemActivePower_Max_B : Double.valueOf(newReceiveDataModel.grid_active_power_b);
        reportDateModel.systemActivePower_Max_C = oldReportDateModel.systemActivePower_Max_C > Double.valueOf(newReceiveDataModel.grid_active_power_c) ? oldReportDateModel.systemActivePower_Max_C : Double.valueOf(newReceiveDataModel.grid_active_power_c);

        reportDateModel.systemActivePower_Min_A = oldReportDateModel.systemActivePower_Min_A > Double.valueOf(newReceiveDataModel.grid_active_power_a) ? oldReportDateModel.systemActivePower_Min_A : Double.valueOf(newReceiveDataModel.grid_active_power_a);
        reportDateModel.systemActivePower_Min_B = oldReportDateModel.systemActivePower_Min_B > Double.valueOf(newReceiveDataModel.grid_active_power_b) ? oldReportDateModel.systemActivePower_Min_B : Double.valueOf(newReceiveDataModel.grid_active_power_b);
        reportDateModel.systemActivePower_Min_C = oldReportDateModel.systemActivePower_Min_C > Double.valueOf(newReceiveDataModel.grid_active_power_c) ? oldReportDateModel.systemActivePower_Min_C : Double.valueOf(newReceiveDataModel.grid_active_power_c);

        reportDateModel.systemActivePower_AVG_A = DoubleUtils.formatDoubleData(oldReportDateModel.systemActivePower_AVG_A + Double.valueOf(newReceiveDataModel.grid_active_power_a) / 2, 2);
        reportDateModel.systemActivePower_AVG_B = DoubleUtils.formatDoubleData(oldReportDateModel.systemActivePower_AVG_B + Double.valueOf(newReceiveDataModel.grid_active_power_b) / 2, 2);
        reportDateModel.systemActivePower_AVG_C = DoubleUtils.formatDoubleData(oldReportDateModel.systemActivePower_AVG_C + Double.valueOf(newReceiveDataModel.grid_active_power_c) / 2, 2);

        reportDateModel.systemReActivePower_Max_A = oldReportDateModel.systemReActivePower_Max_A > Double.valueOf(newReceiveDataModel.grid_reactive_power_a) ? oldReportDateModel.systemReActivePower_Max_A : Double.valueOf(newReceiveDataModel.grid_reactive_power_a);
        reportDateModel.systemReActivePower_Max_B = oldReportDateModel.systemReActivePower_Max_B > Double.valueOf(newReceiveDataModel.grid_reactive_power_b) ? oldReportDateModel.systemReActivePower_Max_B : Double.valueOf(newReceiveDataModel.grid_reactive_power_b);
        reportDateModel.systemReActivePower_Max_C = oldReportDateModel.systemReActivePower_Max_C > Double.valueOf(newReceiveDataModel.grid_reactive_power_c) ? oldReportDateModel.systemReActivePower_Max_C : Double.valueOf(newReceiveDataModel.grid_reactive_power_c);
        reportDateModel.systemReActivePower_Min_A = oldReportDateModel.systemReActivePower_Min_A > Double.valueOf(newReceiveDataModel.grid_reactive_power_a) ? oldReportDateModel.systemReActivePower_Min_A : Double.valueOf(newReceiveDataModel.grid_reactive_power_a);
        reportDateModel.systemReActivePower_Min_B = oldReportDateModel.systemReActivePower_Min_B > Double.valueOf(newReceiveDataModel.grid_reactive_power_b) ? oldReportDateModel.systemReActivePower_Min_B : Double.valueOf(newReceiveDataModel.grid_reactive_power_b);
        reportDateModel.systemReActivePower_Min_C = oldReportDateModel.systemReActivePower_Min_C > Double.valueOf(newReceiveDataModel.grid_reactive_power_c) ? oldReportDateModel.systemReActivePower_Min_C : Double.valueOf(newReceiveDataModel.grid_reactive_power_c);

        reportDateModel.systemReActivePower_AVG_A = DoubleUtils.formatDoubleData(oldReportDateModel.systemReActivePower_AVG_A + Double.valueOf(newReceiveDataModel.grid_reactive_power_a) / 2, 2);
        reportDateModel.systemReActivePower_AVG_B = DoubleUtils.formatDoubleData(oldReportDateModel.systemReActivePower_AVG_B + Double.valueOf(newReceiveDataModel.grid_reactive_power_b) / 2, 2);
        reportDateModel.systemReActivePower_AVG_C = DoubleUtils.formatDoubleData(oldReportDateModel.systemReActivePower_AVG_C + Double.valueOf(newReceiveDataModel.grid_reactive_power_c) / 2, 2);

        int new_systemEventValue = (newReceiveDataModel.errorCode_1_real == 0) && (newReceiveDataModel.errorCode_2_real == 0) && (newReceiveDataModel.errorCode_3_real == 0) && (newReceiveDataModel.errorCode_4_real == 0) && (newReceiveDataModel.errorCode_5_real == 0) ? 0 : 1;
        reportDateModel.systemEvent = (oldReportDateModel.systemEvent == 0) && new_systemEventValue == 0 ? 0 : 1;

        return reportDateModel;
    }

    private long getCurrentData() {
        return  System.currentTimeMillis();
    }
/**
 * 计算10s 数据库的数据，然后 计算出 一小时的 最大值小平均
 * */

    public static ReportDateModel calculate_10s_ReceiveDataModel(List<ReceiveDataModel_10s> receiveDataModel10sList) {

        int length = receiveDataModel10sList.size();

        ReportDateModel reportDateModel = new ReportDateModel();
        reportDateModel.date = receiveDataModel10sList.get(0).date;
        reportDateModel.systemPF_Max_All = receiveDataModel10sList.get(0).all_PF;
        reportDateModel.systemPF_Min_All = receiveDataModel10sList.get(0).all_PF;

        reportDateModel.systemPF_Max_A = receiveDataModel10sList.get(0).grid_PF_a;
        reportDateModel.systemPF_Max_B = receiveDataModel10sList.get(0).grid_PF_b;
        reportDateModel.systemPF_Max_C = receiveDataModel10sList.get(0).grid_PF_c;

        reportDateModel.systemPF_Min_A = receiveDataModel10sList.get(0).grid_PF_a;
        reportDateModel.systemPF_Min_B = receiveDataModel10sList.get(0).grid_PF_b;
        reportDateModel.systemPF_Min_C = receiveDataModel10sList.get(0).grid_PF_c;

        reportDateModel.loadPF_Max_A = receiveDataModel10sList.get(0).load_power_factor_a;
        reportDateModel.loadPF_Max_B = receiveDataModel10sList.get(0).load_power_factor_b;
        reportDateModel.loadPF_Max_C = receiveDataModel10sList.get(0).load_power_factor_c;

        reportDateModel.loadPF_Min_A = receiveDataModel10sList.get(0).load_power_factor_a;
        reportDateModel.loadPF_Min_B = receiveDataModel10sList.get(0).load_power_factor_b;
        reportDateModel.loadPF_Min_C = receiveDataModel10sList.get(0).load_power_factor_c;

        reportDateModel.systemActivePower_Max_A = receiveDataModel10sList.get(0).grid_active_power_a;
        reportDateModel.systemActivePower_Max_B = receiveDataModel10sList.get(0).grid_active_power_b;
        reportDateModel.systemActivePower_Max_C = receiveDataModel10sList.get(0).grid_active_power_c;

        reportDateModel.systemActivePower_Min_A = receiveDataModel10sList.get(0).grid_active_power_a;
        reportDateModel.systemActivePower_Min_B = receiveDataModel10sList.get(0).grid_active_power_b;
        reportDateModel.systemActivePower_Min_C = receiveDataModel10sList.get(0).grid_active_power_c;

        reportDateModel.systemReActivePower_Max_A = receiveDataModel10sList.get(0).grid_reactive_power_a;
        reportDateModel.systemReActivePower_Max_B = receiveDataModel10sList.get(0).grid_reactive_power_b;
        reportDateModel.systemReActivePower_Max_C = receiveDataModel10sList.get(0).grid_reactive_power_c;
        reportDateModel.systemReActivePower_Min_A = receiveDataModel10sList.get(0).grid_reactive_power_a;
        reportDateModel.systemReActivePower_Min_B = receiveDataModel10sList.get(0).grid_reactive_power_b;
        reportDateModel.systemReActivePower_Min_C = receiveDataModel10sList.get(0).grid_reactive_power_c;


        double systemPF_AVG_All = 0;
        double systemPF_AVG_A = 0;
        double systemPF_AVG_B = 0;
        double systemPF_AVG_C = 0;

        double loadPF_AVG_A = 0;
        double loadPF_AVG_B = 0;
        double loadPF_AVG_C = 0;

        double systemActivePower_AVG_A = 0;
        double systemActivePower_AVG_B = 0;
        double systemActivePower_AVG_C = 0;

        double systemReActivePower_AVG_A = 0;
        double systemReActivePower_AVG_B = 0;
        double systemReActivePower_AVG_C = 0;

        boolean systemEventFlag = false;

//        List<Double> list_all_PF = new ArrayList<>();
//        List<Double> list_grid_PF_a = new ArrayList<>();
//        List<Double> list_grid_PF_b = new ArrayList<>();
//        List<Double> list_grid_PF_c = new ArrayList<>();
//
//        List<Double> list_load_power_factor_a = new ArrayList<>();
//        List<Double> list_load_power_factor_b = new ArrayList<>();
//        List<Double> list_load_power_factor_c = new ArrayList<>();
//
//        List<Double> list_grid_active_power_a = new ArrayList<>();
//        List<Double> list_grid_active_power_b = new ArrayList<>();
//        List<Double> list_grid_active_power_c = new ArrayList<>();
//
//        List<Double> list_grid_reactive_power_a = new ArrayList<>();
//        List<Double> list_grid_reactive_power_b = new ArrayList<>();
//        List<Double> list_grid_reactive_power_c = new ArrayList<>();


        for (ReceiveDataModel_10s receiveDataModel_10s : receiveDataModel10sList) {
            reportDateModel.systemPF_Max_All = reportDateModel.systemPF_Max_All > receiveDataModel_10s.all_PF ? reportDateModel.systemPF_Max_All : receiveDataModel_10s.all_PF;
            reportDateModel.systemPF_Min_All = reportDateModel.systemPF_Min_All < receiveDataModel_10s.all_PF ? reportDateModel.systemPF_Min_All : receiveDataModel_10s.all_PF;

            reportDateModel.date_systemPF_Max_All = reportDateModel.systemPF_Max_All > receiveDataModel_10s.all_PF ? reportDateModel.date : receiveDataModel_10s.date;
            reportDateModel.date_systemPF_Min_All = reportDateModel.systemPF_Min_All < receiveDataModel_10s.all_PF ? reportDateModel.date : receiveDataModel_10s.date;
            systemPF_AVG_All = systemPF_AVG_All + receiveDataModel_10s.all_PF;


            reportDateModel.systemPF_Max_A = reportDateModel.systemPF_Max_A > receiveDataModel_10s.grid_PF_a ? reportDateModel.systemPF_Max_A : receiveDataModel_10s.grid_PF_a;
            reportDateModel.systemPF_Max_B = reportDateModel.systemPF_Max_B > receiveDataModel_10s.grid_PF_b ? reportDateModel.systemPF_Max_B : receiveDataModel_10s.grid_PF_b;
            reportDateModel.systemPF_Max_C = reportDateModel.systemPF_Max_C > receiveDataModel_10s.grid_PF_c ? reportDateModel.systemPF_Max_C : receiveDataModel_10s.grid_PF_c;

            reportDateModel.systemPF_Min_A = reportDateModel.systemPF_Min_A < receiveDataModel_10s.grid_PF_a ? reportDateModel.systemPF_Min_A : receiveDataModel_10s.grid_PF_a;
            reportDateModel.systemPF_Min_B = reportDateModel.systemPF_Min_B < receiveDataModel_10s.grid_PF_b ? reportDateModel.systemPF_Min_B : receiveDataModel_10s.grid_PF_b;
            reportDateModel.systemPF_Min_C = reportDateModel.systemPF_Min_C < receiveDataModel_10s.grid_PF_c ? reportDateModel.systemPF_Min_C : receiveDataModel_10s.grid_PF_c;

            systemPF_AVG_A = systemPF_AVG_A + receiveDataModel_10s.grid_PF_a;
            systemPF_AVG_B = systemPF_AVG_B + receiveDataModel_10s.grid_PF_b;
            systemPF_AVG_C = systemPF_AVG_C + receiveDataModel_10s.grid_PF_c;


            reportDateModel.loadPF_Max_A = reportDateModel.loadPF_Max_A > receiveDataModel_10s.load_power_factor_a ? reportDateModel.loadPF_Max_A : receiveDataModel_10s.load_power_factor_a;
            reportDateModel.loadPF_Max_B = reportDateModel.loadPF_Max_B > receiveDataModel_10s.load_power_factor_b ? reportDateModel.loadPF_Max_B : receiveDataModel_10s.load_power_factor_b;
            reportDateModel.loadPF_Max_C = reportDateModel.loadPF_Max_C > receiveDataModel_10s.load_power_factor_c ? reportDateModel.loadPF_Max_C : receiveDataModel_10s.load_power_factor_c;

            reportDateModel.loadPF_Min_A = reportDateModel.loadPF_Min_A < receiveDataModel_10s.load_power_factor_a ? reportDateModel.loadPF_Min_A : receiveDataModel_10s.load_power_factor_a;
            reportDateModel.loadPF_Min_B = reportDateModel.loadPF_Min_B < receiveDataModel_10s.load_power_factor_b ? reportDateModel.loadPF_Min_B : receiveDataModel_10s.load_power_factor_b;
            reportDateModel.loadPF_Min_C = reportDateModel.loadPF_Min_C < receiveDataModel_10s.load_power_factor_c ? reportDateModel.loadPF_Min_C : receiveDataModel_10s.load_power_factor_c;

            loadPF_AVG_A = loadPF_AVG_A + receiveDataModel_10s.load_power_factor_a;
            loadPF_AVG_B = loadPF_AVG_B + receiveDataModel_10s.load_power_factor_b;
            loadPF_AVG_C = loadPF_AVG_C + receiveDataModel_10s.load_power_factor_c;

            reportDateModel.systemActivePower_Max_A = reportDateModel.systemActivePower_Max_A > receiveDataModel_10s.grid_active_power_a ? reportDateModel.systemActivePower_Max_A : receiveDataModel_10s.grid_active_power_a;
            reportDateModel.systemActivePower_Max_B = reportDateModel.systemActivePower_Max_B > receiveDataModel_10s.grid_active_power_b ? reportDateModel.systemActivePower_Max_B : receiveDataModel_10s.grid_active_power_b;
            reportDateModel.systemActivePower_Max_C = reportDateModel.systemActivePower_Max_C > receiveDataModel_10s.grid_active_power_c ? reportDateModel.systemActivePower_Max_C : receiveDataModel_10s.grid_active_power_c;

            reportDateModel.systemActivePower_Min_A = reportDateModel.systemActivePower_Min_A < receiveDataModel_10s.grid_active_power_a ? reportDateModel.systemActivePower_Min_A : receiveDataModel_10s.grid_active_power_a;
            reportDateModel.systemActivePower_Min_B = reportDateModel.systemActivePower_Min_B < receiveDataModel_10s.grid_active_power_b ? reportDateModel.systemActivePower_Min_B : receiveDataModel_10s.grid_active_power_b;
            reportDateModel.systemActivePower_Min_C = reportDateModel.systemActivePower_Min_C < receiveDataModel_10s.grid_active_power_c ? reportDateModel.systemActivePower_Min_C : receiveDataModel_10s.grid_active_power_c;


            systemActivePower_AVG_A = systemActivePower_AVG_A + receiveDataModel_10s.grid_active_power_a;
            systemActivePower_AVG_B = systemActivePower_AVG_B + receiveDataModel_10s.grid_active_power_b;
            systemActivePower_AVG_C = systemActivePower_AVG_C + receiveDataModel_10s.grid_active_power_c;

            reportDateModel.systemReActivePower_Max_A = reportDateModel.systemReActivePower_Max_A > receiveDataModel_10s.grid_reactive_power_a ? reportDateModel.systemReActivePower_Max_A : receiveDataModel_10s.grid_reactive_power_a;
            reportDateModel.systemReActivePower_Max_B = reportDateModel.systemReActivePower_Max_B > receiveDataModel_10s.grid_reactive_power_b ? reportDateModel.systemReActivePower_Max_B : receiveDataModel_10s.grid_reactive_power_b;
            reportDateModel.systemReActivePower_Max_C = reportDateModel.systemReActivePower_Max_C > receiveDataModel_10s.grid_reactive_power_c ? reportDateModel.systemReActivePower_Max_C : receiveDataModel_10s.grid_reactive_power_c;
            reportDateModel.systemReActivePower_Min_A = reportDateModel.systemReActivePower_Min_A < receiveDataModel_10s.grid_reactive_power_a ? reportDateModel.systemReActivePower_Min_A : receiveDataModel_10s.grid_reactive_power_a;
            reportDateModel.systemReActivePower_Min_B = reportDateModel.systemReActivePower_Min_B < receiveDataModel_10s.grid_reactive_power_b ? reportDateModel.systemReActivePower_Min_B : receiveDataModel_10s.grid_reactive_power_b;
            reportDateModel.systemReActivePower_Min_C = reportDateModel.systemReActivePower_Min_C < receiveDataModel_10s.grid_reactive_power_c ? reportDateModel.systemReActivePower_Min_C : receiveDataModel_10s.grid_reactive_power_c;

            systemReActivePower_AVG_A = systemReActivePower_AVG_A + receiveDataModel_10s.grid_reactive_power_a;
            systemReActivePower_AVG_B = systemReActivePower_AVG_B + receiveDataModel_10s.grid_reactive_power_b;
            systemReActivePower_AVG_C = systemReActivePower_AVG_C + receiveDataModel_10s.grid_reactive_power_c;

            systemEventFlag = (systemEventFlag | receiveDataModel_10s.runningEvent);
//
//            list_all_PF.add(Double.valueOf(receiveDataModel_10s.all_PF));
//
//            list_grid_PF_a.add(Double.valueOf(receiveDataModel_10s.grid_PF_a));
//            list_grid_PF_b.add(Double.valueOf(receiveDataModel_10s.grid_PF_b));
//            list_grid_PF_c.add(Double.valueOf(receiveDataModel_10s.grid_PF_c));
//
//            list_load_power_factor_a.add((Double.valueOf(receiveDataModel_10s.load_power_factor_a)));
//            list_load_power_factor_b.add((Double.valueOf(receiveDataModel_10s.load_power_factor_b)));
//            list_load_power_factor_c.add((Double.valueOf(receiveDataModel_10s.load_power_factor_c)));
//
//
//            list_grid_active_power_a.add(Double.valueOf(receiveDataModel_10s.grid_active_power_a));
//            list_grid_active_power_b.add(Double.valueOf(receiveDataModel_10s.grid_active_power_b));
//            list_grid_active_power_c.add(Double.valueOf(receiveDataModel_10s.grid_active_power_c));
//
//            list_grid_reactive_power_a.add(Double.valueOf(receiveDataModel_10s.grid_reactive_power_a));
//            list_grid_reactive_power_b.add(Double.valueOf(receiveDataModel_10s.grid_reactive_power_b));
//            list_grid_reactive_power_c.add(Double.valueOf(receiveDataModel_10s.grid_reactive_power_c));

        }

        int scale=3;
        reportDateModel.systemPF_AVG_All=DoubleUtils.formatDoubleData(systemPF_AVG_All/length,scale);

        reportDateModel.systemPF_AVG_A = DoubleUtils.formatDoubleData(systemPF_AVG_A / length, scale);
        reportDateModel.systemPF_AVG_B = DoubleUtils.formatDoubleData(systemPF_AVG_B / length, scale);
        reportDateModel.systemPF_AVG_C = DoubleUtils.formatDoubleData(systemPF_AVG_C / length, scale);

        reportDateModel.loadPF_AVG_A = DoubleUtils.formatDoubleData(loadPF_AVG_A / length, scale);
        reportDateModel.loadPF_AVG_B = DoubleUtils.formatDoubleData(loadPF_AVG_B / length, scale);
        reportDateModel.loadPF_AVG_C = DoubleUtils.formatDoubleData(loadPF_AVG_C / length, scale);

        reportDateModel.systemActivePower_AVG_A = DoubleUtils.formatDoubleData(systemActivePower_AVG_A / length, scale);
        reportDateModel.systemActivePower_AVG_B = DoubleUtils.formatDoubleData(systemActivePower_AVG_B / length, scale);
        reportDateModel.systemActivePower_AVG_C = DoubleUtils.formatDoubleData(systemActivePower_AVG_C / length, scale);

        reportDateModel.systemReActivePower_AVG_A = DoubleUtils.formatDoubleData(systemReActivePower_AVG_A / length, scale);
        reportDateModel.systemReActivePower_AVG_B = DoubleUtils.formatDoubleData(systemReActivePower_AVG_B / length, scale);
        reportDateModel.systemReActivePower_AVG_C = DoubleUtils.formatDoubleData(systemReActivePower_AVG_C / length, scale);

        reportDateModel.systemEvent = systemEventFlag ? 1 : 0;
        return reportDateModel;
    }

    /**
     * 计算1h 数据库的数据，然后 计算出 一小时的 最大值小平均
     * */

    public static ReportDateModel_1day calculate_1h_ReceiveDataModel_1day(List<ReportDateModel> reportDateModels_1h) {

        int length = reportDateModels_1h.size();

        ReportDateModel_1day reportDateModel_1day = new ReportDateModel_1day();
        reportDateModel_1day.date = reportDateModels_1h.get(0).date;
        reportDateModel_1day.systemPF_Max_All = reportDateModels_1h.get(0).systemPF_Max_All;
        reportDateModel_1day.systemPF_Min_All = reportDateModels_1h.get(0).systemPF_Min_All;

        reportDateModel_1day.systemPF_Max_A = reportDateModels_1h.get(0).systemPF_Max_A ;
        reportDateModel_1day.systemPF_Max_B = reportDateModels_1h.get(0).systemPF_Max_B ;
        reportDateModel_1day.systemPF_Max_C = reportDateModels_1h.get(0).systemPF_Max_C ;

        reportDateModel_1day.systemPF_Min_A = reportDateModels_1h.get(0).systemPF_Min_A;
        reportDateModel_1day.systemPF_Min_B = reportDateModels_1h.get(0).systemPF_Min_B;
        reportDateModel_1day.systemPF_Min_C = reportDateModels_1h.get(0).systemPF_Min_C;

        reportDateModel_1day.loadPF_Max_A = reportDateModels_1h.get(0).loadPF_Max_A;
        reportDateModel_1day.loadPF_Max_B = reportDateModels_1h.get(0).loadPF_Max_B;
        reportDateModel_1day.loadPF_Max_C = reportDateModels_1h.get(0).loadPF_Max_C;
        reportDateModel_1day.loadPF_Min_A = reportDateModels_1h.get(0).loadPF_Min_A;
        reportDateModel_1day.loadPF_Min_B = reportDateModels_1h.get(0).loadPF_Min_B;
        reportDateModel_1day.loadPF_Min_C = reportDateModels_1h.get(0).loadPF_Min_C;

        reportDateModel_1day.systemActivePower_Max_A = reportDateModels_1h.get(0).systemActivePower_Max_A;
        reportDateModel_1day.systemActivePower_Max_B = reportDateModels_1h.get(0).systemActivePower_Max_B;
        reportDateModel_1day.systemActivePower_Max_C = reportDateModels_1h.get(0).systemActivePower_Max_C;
        reportDateModel_1day.systemActivePower_Min_A = reportDateModels_1h.get(0).systemActivePower_Min_A;
        reportDateModel_1day.systemActivePower_Min_B = reportDateModels_1h.get(0).systemActivePower_Min_B;
        reportDateModel_1day.systemActivePower_Min_C = reportDateModels_1h.get(0).systemActivePower_Min_C;

        reportDateModel_1day.systemReActivePower_Max_A = reportDateModels_1h.get(0).systemReActivePower_Max_A;
        reportDateModel_1day.systemReActivePower_Max_B = reportDateModels_1h.get(0).systemReActivePower_Max_B;
        reportDateModel_1day.systemReActivePower_Max_C = reportDateModels_1h.get(0).systemReActivePower_Max_C;
        reportDateModel_1day.systemReActivePower_Min_A = reportDateModels_1h.get(0).systemReActivePower_Min_A;
        reportDateModel_1day.systemReActivePower_Min_B = reportDateModels_1h.get(0).systemReActivePower_Min_B;
        reportDateModel_1day.systemReActivePower_Min_C = reportDateModels_1h.get(0).systemReActivePower_Min_C;


        double systemPF_AVG_All = 0;
        double systemPF_AVG_A = 0;
        double systemPF_AVG_B = 0;
        double systemPF_AVG_C = 0;

        double loadPF_AVG_A = 0;
        double loadPF_AVG_B = 0;
        double loadPF_AVG_C = 0;

        double systemActivePower_AVG_A = 0;
        double systemActivePower_AVG_B = 0;
        double systemActivePower_AVG_C = 0;

        double systemReActivePower_AVG_A = 0;
        double systemReActivePower_AVG_B = 0;
        double systemReActivePower_AVG_C = 0;




        for (ReportDateModel reportDateModel1 : reportDateModels_1h) {
            reportDateModel_1day.systemPF_Max_All = reportDateModel_1day.systemPF_Max_All > reportDateModel1.systemPF_Max_All ? reportDateModel_1day.systemPF_Max_All : reportDateModel1.systemPF_Max_All;
            reportDateModel_1day.systemPF_Min_All = reportDateModel_1day.systemPF_Min_All < reportDateModel1.systemPF_Min_All ? reportDateModel_1day.systemPF_Min_All : reportDateModel1.systemPF_Min_All;

            reportDateModel_1day.date_systemPF_Max_All = reportDateModel_1day.systemPF_Max_All > reportDateModel1.systemPF_Max_All ? reportDateModel_1day.date : reportDateModel1.date;
            reportDateModel_1day.date_systemPF_Min_All = reportDateModel_1day.systemPF_Min_All < reportDateModel1.systemPF_Min_All ? reportDateModel_1day.date : reportDateModel1.date;
            systemPF_AVG_All = systemPF_AVG_All + reportDateModel1.systemPF_AVG_All;


            reportDateModel_1day.systemPF_Max_A = reportDateModel_1day.systemPF_Max_A > reportDateModel1.systemPF_Max_A ? reportDateModel_1day.systemPF_Max_A : reportDateModel1.systemPF_Max_A;
            reportDateModel_1day.systemPF_Max_B = reportDateModel_1day.systemPF_Max_B > reportDateModel1.systemPF_Max_B ? reportDateModel_1day.systemPF_Max_B : reportDateModel1.systemPF_Max_B;
            reportDateModel_1day.systemPF_Max_C = reportDateModel_1day.systemPF_Max_C > reportDateModel1.systemPF_Max_C ? reportDateModel_1day.systemPF_Max_C : reportDateModel1.systemPF_Max_C;
            reportDateModel_1day.systemPF_Min_A = reportDateModel_1day.systemPF_Min_A < reportDateModel1.systemPF_Min_A ? reportDateModel_1day.systemPF_Min_A : reportDateModel1.systemPF_Min_A;
            reportDateModel_1day.systemPF_Min_B = reportDateModel_1day.systemPF_Min_B < reportDateModel1.systemPF_Min_B ? reportDateModel_1day.systemPF_Min_B : reportDateModel1.systemPF_Min_B;
            reportDateModel_1day.systemPF_Min_C = reportDateModel_1day.systemPF_Min_C < reportDateModel1.systemPF_Min_C ? reportDateModel_1day.systemPF_Min_C : reportDateModel1.systemPF_Min_C;

            systemPF_AVG_A = systemPF_AVG_A + reportDateModel1.systemPF_AVG_A;
            systemPF_AVG_B = systemPF_AVG_B + reportDateModel1.systemPF_AVG_B;
            systemPF_AVG_C = systemPF_AVG_C + reportDateModel1.systemPF_AVG_C;


            reportDateModel_1day.loadPF_Max_A = reportDateModel_1day.loadPF_Max_A > reportDateModel1.loadPF_Max_A ? reportDateModel_1day.loadPF_Max_A : reportDateModel1.loadPF_Max_A;
            reportDateModel_1day.loadPF_Max_B = reportDateModel_1day.loadPF_Max_B > reportDateModel1.loadPF_Max_B ? reportDateModel_1day.loadPF_Max_B : reportDateModel1.loadPF_Max_B;
            reportDateModel_1day.loadPF_Max_C = reportDateModel_1day.loadPF_Max_C > reportDateModel1.loadPF_Max_C ? reportDateModel_1day.loadPF_Max_C : reportDateModel1.loadPF_Max_C;
            reportDateModel_1day.loadPF_Min_A = reportDateModel_1day.loadPF_Min_A < reportDateModel1.loadPF_Min_A ? reportDateModel_1day.loadPF_Min_A : reportDateModel1.loadPF_Min_A;
            reportDateModel_1day.loadPF_Min_B = reportDateModel_1day.loadPF_Min_B < reportDateModel1.loadPF_Min_B ? reportDateModel_1day.loadPF_Min_B : reportDateModel1.loadPF_Min_B;
            reportDateModel_1day.loadPF_Min_C = reportDateModel_1day.loadPF_Min_C < reportDateModel1.loadPF_Min_C ? reportDateModel_1day.loadPF_Min_C : reportDateModel1.loadPF_Min_C;

            loadPF_AVG_A = loadPF_AVG_A + reportDateModel1.loadPF_AVG_A;
            loadPF_AVG_B = loadPF_AVG_B + reportDateModel1.loadPF_AVG_B;
            loadPF_AVG_C = loadPF_AVG_C + reportDateModel1.loadPF_AVG_C;

            reportDateModel_1day.systemActivePower_Max_A = reportDateModel_1day.systemActivePower_Max_A > reportDateModel1.systemActivePower_Max_A ? reportDateModel_1day.systemActivePower_Max_A : reportDateModel1.systemActivePower_Max_A;
            reportDateModel_1day.systemActivePower_Max_B = reportDateModel_1day.systemActivePower_Max_B > reportDateModel1.systemActivePower_Max_B ? reportDateModel_1day.systemActivePower_Max_B : reportDateModel1.systemActivePower_Max_B;
            reportDateModel_1day.systemActivePower_Max_C = reportDateModel_1day.systemActivePower_Max_C > reportDateModel1.systemActivePower_Max_C ? reportDateModel_1day.systemActivePower_Max_C : reportDateModel1.systemActivePower_Max_C;
            reportDateModel_1day.systemActivePower_Min_A = reportDateModel_1day.systemActivePower_Min_A < reportDateModel1.systemActivePower_Min_A ? reportDateModel_1day.systemActivePower_Min_A : reportDateModel1.systemActivePower_Min_A;
            reportDateModel_1day.systemActivePower_Min_B = reportDateModel_1day.systemActivePower_Min_B < reportDateModel1.systemActivePower_Min_B ? reportDateModel_1day.systemActivePower_Min_B : reportDateModel1.systemActivePower_Min_B;
            reportDateModel_1day.systemActivePower_Min_C = reportDateModel_1day.systemActivePower_Min_C < reportDateModel1.systemActivePower_Min_C ? reportDateModel_1day.systemActivePower_Min_C : reportDateModel1.systemActivePower_Min_C;


            systemActivePower_AVG_A = systemActivePower_AVG_A + reportDateModel1.systemActivePower_AVG_A;
            systemActivePower_AVG_B = systemActivePower_AVG_B + reportDateModel1.systemActivePower_AVG_B;
            systemActivePower_AVG_C = systemActivePower_AVG_C + reportDateModel1.systemActivePower_AVG_C;

            reportDateModel_1day.systemReActivePower_Max_A = reportDateModel_1day.systemReActivePower_Max_A > reportDateModel1.systemReActivePower_Max_A ? reportDateModel_1day.systemReActivePower_Max_A : reportDateModel1.systemReActivePower_Max_A;
            reportDateModel_1day.systemReActivePower_Max_B = reportDateModel_1day.systemReActivePower_Max_B > reportDateModel1.systemReActivePower_Max_B ? reportDateModel_1day.systemReActivePower_Max_B : reportDateModel1.systemReActivePower_Max_B;
            reportDateModel_1day.systemReActivePower_Max_C = reportDateModel_1day.systemReActivePower_Max_C > reportDateModel1.systemReActivePower_Max_C ? reportDateModel_1day.systemReActivePower_Max_C : reportDateModel1.systemReActivePower_Max_C;
            reportDateModel_1day.systemReActivePower_Min_A = reportDateModel_1day.systemReActivePower_Min_A < reportDateModel1.systemReActivePower_Min_A ? reportDateModel_1day.systemReActivePower_Min_A : reportDateModel1.systemReActivePower_Min_A;
            reportDateModel_1day.systemReActivePower_Min_B = reportDateModel_1day.systemReActivePower_Min_B < reportDateModel1.systemReActivePower_Min_B ? reportDateModel_1day.systemReActivePower_Min_B : reportDateModel1.systemReActivePower_Min_B;
            reportDateModel_1day.systemReActivePower_Min_C = reportDateModel_1day.systemReActivePower_Min_C < reportDateModel1.systemReActivePower_Min_C ? reportDateModel_1day.systemReActivePower_Min_C : reportDateModel1.systemReActivePower_Min_C;

            systemReActivePower_AVG_A = systemReActivePower_AVG_A + reportDateModel1.systemReActivePower_AVG_A;
            systemReActivePower_AVG_B = systemReActivePower_AVG_B + reportDateModel1.systemReActivePower_AVG_B;
            systemReActivePower_AVG_C = systemReActivePower_AVG_C + reportDateModel1.systemReActivePower_AVG_C;

            reportDateModel_1day.systemEvent =reportDateModel_1day.systemEvent|reportDateModel1.systemEvent;

        }

        int scale=3;
        reportDateModel_1day.systemPF_AVG_All=DoubleUtils.formatDoubleData(systemPF_AVG_All/length,scale);

        reportDateModel_1day.systemPF_AVG_A = DoubleUtils.formatDoubleData(systemPF_AVG_A / length, scale);
        reportDateModel_1day.systemPF_AVG_B = DoubleUtils.formatDoubleData(systemPF_AVG_B / length, scale);
        reportDateModel_1day.systemPF_AVG_C = DoubleUtils.formatDoubleData(systemPF_AVG_C / length, scale);

        reportDateModel_1day.loadPF_AVG_A = DoubleUtils.formatDoubleData(loadPF_AVG_A / length, scale);
        reportDateModel_1day.loadPF_AVG_B = DoubleUtils.formatDoubleData(loadPF_AVG_B / length, scale);
        reportDateModel_1day.loadPF_AVG_C = DoubleUtils.formatDoubleData(loadPF_AVG_C / length, scale);

        reportDateModel_1day.systemActivePower_AVG_A = DoubleUtils.formatDoubleData(systemActivePower_AVG_A / length, scale);
        reportDateModel_1day.systemActivePower_AVG_B = DoubleUtils.formatDoubleData(systemActivePower_AVG_B / length, scale);
        reportDateModel_1day.systemActivePower_AVG_C = DoubleUtils.formatDoubleData(systemActivePower_AVG_C / length, scale);

        reportDateModel_1day.systemReActivePower_AVG_A = DoubleUtils.formatDoubleData(systemReActivePower_AVG_A / length, scale);
        reportDateModel_1day.systemReActivePower_AVG_B = DoubleUtils.formatDoubleData(systemReActivePower_AVG_B / length, scale);
        reportDateModel_1day.systemReActivePower_AVG_C = DoubleUtils.formatDoubleData(systemReActivePower_AVG_C / length, scale);


        return reportDateModel_1day;
    }



    public  static ReceiveDataModel_10s getReceiveDataModel_10s(ReceiveDataModel receiveDataModel) {

        ReceiveDataModel_10s receiveDataModel_10s = new ReceiveDataModel_10s();
        receiveDataModel_10s.date = System.currentTimeMillis();
        try {
            receiveDataModel_10s.all_PF = Double.valueOf(receiveDataModel.all_PF);
        } catch (Exception e) {
            e.printStackTrace();
        }
        receiveDataModel_10s.grid_active_power_a = Double.valueOf(receiveDataModel.grid_active_power_a);
        receiveDataModel_10s.grid_active_power_b = Double.valueOf(receiveDataModel.grid_active_power_b);
        receiveDataModel_10s.grid_active_power_c = Double.valueOf(receiveDataModel.grid_active_power_c);

        receiveDataModel_10s.grid_reactive_power_a = Double.valueOf(receiveDataModel.grid_reactive_power_a);
        receiveDataModel_10s.grid_reactive_power_b = Double.valueOf(receiveDataModel.grid_reactive_power_b);
        receiveDataModel_10s.grid_reactive_power_c = Double.valueOf(receiveDataModel.grid_reactive_power_c);

        receiveDataModel_10s.grid_PF_a = Double.valueOf(receiveDataModel.grid_PF_a);
        receiveDataModel_10s.grid_PF_b = Double.valueOf(receiveDataModel.grid_PF_b);
        receiveDataModel_10s.grid_PF_c = Double.valueOf(receiveDataModel.grid_PF_c);

        receiveDataModel_10s.load_power_factor_a = Double.valueOf(receiveDataModel.load_power_factor_a);
        receiveDataModel_10s.load_power_factor_b = Double.valueOf(receiveDataModel.load_power_factor_b);
        receiveDataModel_10s.load_power_factor_c = Double.valueOf(receiveDataModel.load_power_factor_c);

        receiveDataModel_10s.runningEvent = (receiveDataModel.alertCode_1_real == 0) && (receiveDataModel.alertCode_2_real == 0) && (receiveDataModel.alertCode_3_real == 0) && (receiveDataModel.alertCode_4_real == 0);
        return receiveDataModel_10s;
    }
}
